package com.zz.admin.common.init;

import cn.hutool.core.util.ObjectUtil;
import com.zz.admin.roleManagement.dto.RoleDto;
import com.zz.admin.roleManagement.service.RoleService;
import com.zz.admin.userManagement.dto.UserDto;
import com.zz.admin.userManagement.dto.UserRoleDto;
import com.zz.admin.userManagement.service.UserRoleService;
import com.zz.admin.userManagement.service.UserService;
import com.zz.common.constants.BaseConstant;
import com.zz.common.utils.convert.LocalDateTimeUtil;
import jakarta.annotation.Resource;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

@Component
public class AdminUserInitializer implements ApplicationRunner {

    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;
    @Resource
    private UserRoleService userRoleService;

    @Resource
    private PasswordEncoder passwordEncoder;

    @Override
    public void run(ApplicationArguments args) {
        UserDto adminUser = userService.getAdminUser();
        if (ObjectUtil.isNotNull(adminUser)) {
            return;
        }
        UserDto userDto = buildAdminUser();
        userService.insertWithId(userDto);
        RoleDto roleDto = buildAdminRole();
        roleService.insertWithId(roleDto);
        userRoleService.insertWithId(buildAdminUserRole());
    }

    // 组装admin用户信息
    private UserDto buildAdminUser() {
        UserDto adminUser = new UserDto();
        adminUser.setUserId("1000");
        adminUser.setUserCode("admin");
        adminUser.setUserName("admin");
        adminUser.setSex("1");
        adminUser.setLoginAccount("admin");
        adminUser.setPassWord(passwordEncoder.encode("admin"));
        adminUser.setStatus(BaseConstant.YES);
        adminUser.setDelFlag(BaseConstant.NO);
        adminUser.setUseSys(BaseConstant.YES);
        adminUser.setFailureTime(LocalDateTimeUtil.convertLocalDateTimeToMilli(LocalDateTimeUtil.nextYearEndTime()));
        adminUser.setCreateBy("system");
        adminUser.setUpdateBy("system");
        adminUser.setCreateDate(LocalDateTimeUtil.now());
        adminUser.setUpdateDate(LocalDateTimeUtil.now());
        return adminUser;
    }

    private RoleDto buildAdminRole() {
        RoleDto adminRole = new RoleDto();
        adminRole.setRoleId("2000");
        adminRole.setRoleName("root");
        adminRole.setRoleCode("root");
        adminRole.setIsManageRole(BaseConstant.YES);
        adminRole.setDelFlag(BaseConstant.NO);
        adminRole.setStatus(BaseConstant.YES);
        adminRole.setCreateBy("system");
        adminRole.setUpdateBy("system");
        adminRole.setCreateDate(LocalDateTimeUtil.now());
        adminRole.setUpdateDate(LocalDateTimeUtil.now());
        return adminRole;
    }

    private UserRoleDto buildAdminUserRole() {
        UserRoleDto userRoleDto = new UserRoleDto();
        userRoleDto.setPkId("3000");
        userRoleDto.setUserId("1000");
        userRoleDto.setRoleId("2000");
        userRoleDto.setUserCode("admin");
        userRoleDto.setRoleCode("root");
        return userRoleDto;
    }

}
